package de.dlrg.wms.wiiDevice.controller;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import motej.Mote;
import motej.MoteFinder;
import motej.MoteFinderListener;
import motej.event.CoreButtonEvent;
import motej.event.CoreButtonListener;
import wms2010.interfaces.devices.IDevice;

import com.intel.bluetooth.BlueCoveConfigProperties;

import de.dlrg.wms.interfaces.controller.IDeviceController;
import de.dlrg.wms.interfaces.controller.IDeviceControllerCallback;
import de.dlrg.wms.interfaces.controller.StopperEvent;
import de.dlrg.wms.wiiDevice.WiiDevice;

@Component
public class WiiMoteDeviceController extends IDeviceController implements
		MoteFinderListener {
	private List<IDevice> devices = new ArrayList<IDevice>();
	private final MoteFinder finder = MoteFinder.getMoteFinder();

	private ArrayList<IDeviceControllerCallback> iDeviceControllerCallbacks = new ArrayList<IDeviceControllerCallback>();

	@Override
	public void registerDeviceControllerCallback(
			IDeviceControllerCallback iDeviceControllerCallback) {
		if (this.iDeviceControllerCallbacks.contains(iDeviceControllerCallback)) {
			throw new ArrayIndexOutOfBoundsException(
					"Already registered such callback! Check your code!");
		}
		this.iDeviceControllerCallbacks.add(iDeviceControllerCallback);
	}

	@Override
	public void removeDeviceControllerCallback(
			IDeviceControllerCallback iDeviceControllerCallback) {
		if (!this.iDeviceControllerCallbacks
				.contains(iDeviceControllerCallback)) {
			throw new ArrayIndexOutOfBoundsException(
					"Not such callback registered! Check your code!");
		}
		this.iDeviceControllerCallbacks.remove(iDeviceControllerCallback);
	}

	private void notifyDeviceControllerCallback() {
		for (IDeviceControllerCallback controllerCallback : this.iDeviceControllerCallbacks) {
			controllerCallback.finishedDiscovery();
		}
	}

	public void moteFound(Mote mote) {
		System.out.println("Found mote: " + mote.getBluetoothAddress());
		mote.rumble(1000l);
		final WiiDevice wiiDevice = new WiiDevice(mote);
		this.devices.add(wiiDevice);
		final Long time = System.currentTimeMillis();
		mote.addCoreButtonListener(new CoreButtonListener() {

			public void buttonPressed(CoreButtonEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.isButtonAPressed() || arg0.isButtonBPressed()) {
					notifyButtonPressed(new StopperEvent().setDevice(wiiDevice)
							.setTimeOfEvent(time));
				}
			}
		});

		this.notifyDeviceControllerCallback();
	}

	@Override
	public void startDiscovery() {

		finder.startDiscovery();
		System.out.println("started discovery");
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(30L * 1000L);
					System.out.println("stopped discovery");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finder.stopDiscovery();
			}
		}.start();
	}

	@Override
	public void stopDiscovery() {
		// TODO Auto-generated method stub

	}

	@Override
	public List<IDevice> getAllDevices() {
		// TODO Auto-generated method stub
		return this.devices;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "WiiMote Bluetooth";
	}

	public WiiMoteDeviceController() {
		System.out.println("started wiimote");
		finder.addMoteFinderListener(this);
	}

}
